$(function () {
  var commonHost = 'http://t.scsj.net.cn/admin'
  // 获取url中code值
  function getUrlParams (name) {
    var search = document.location.search
    var pattern = new RegExp('[?&]' + name + '\=([^&]+)', 'g')
    var matcher = pattern.exec(search)
    var items = null
    if (null != matcher) {
      try {
        items = decodeURIComponent(decodeURIComponent(matcher[1]))
      } catch (e) {
        try {
          items = decodeURIComponent(matcher[1])
        } catch (e) {
          items = matcher[1]
        }
      }
    }
    return items
  }
  var s = getUrlParams('s')
  var goodsId = getUrlParams('goodsId')
  // 解密获取uid goodsId
  if (s) {
    $.ajax({
      url: commonHost + '/API/crypt.php',
      type: 'post',
      data: {type: 'decrypt',str: s},
      success: function (data) {
        data = JSON.parse(data)
        if (data.succ == 1) {
          $.cookie('pid', data.uid, {
            path: '/',
            expires: 30
          })
          $.cookie('goodsId', data.goodsId, {
            path: '/',
            expires: 30
          })
          $.cookie('rid', data.rid, {
            path: '/',
            expires: 30
          })
          // 获取商品信息
          $.ajax({
            type: 'post',
            url: commonHost + '/index.php/mall/get_goods_detail',
            data: {goodsId: data.goodsId,tk: 0},
            success: function (data) {
              data = JSON.parse(data)
              data = data.data
              console.log(data);
              $.cookie('storeId', data.store.id, {
                path: '/',
                expires: 30
              })
              var goodsDetail = data.goodsDetail
              // 截取图文详情中的html内容
              var start = goodsDetail.goodsDesc.indexOf('<body>') + 6
              var end = goodsDetail.goodsDesc.indexOf('</body>')
              // 获取轮播图片并追加到页面
              var banner = ''
              $(goodsDetail.goodsImg).each(function (i, v) {
                banner += "<div class='swiper-slide'>"
                  + '<img src=' + v.img + '>'
                  + '</div>'
                return banner
              })
              $('.swiper-wrapper').html(banner)
              var mySwiper = new Swiper('.swiper-container', {
                pagination: '.swiper-pagination',
                paginationType: 'fraction'
              })
              // 模板展示页面数据
              var html = template('goodsDetail', data)
              $('.main').html(html)
              // 图文详情数据展示
              var time = data.evaluate.gevalAddtime
              time = new Date(time).toLocaleString()
              $('.p-list i').text(time)
              var gevalScores = data.evaluate.gevalScores
              for (var i = 0;i < gevalScores;i++) {
                $('.mmm img').eq(i).attr('src', './images/star_red.png')
              }
              $('.text-image').append(goodsDetail.goodsDesc.substring(start, end))
              $('.smallImg img').attr('src', goodsDetail.goodsImg[0].img)
              $('iframe').html(goodsDetail.goodsDesc)
              $('.goodsPrice').text('￥' + goodsDetail.goodsPrice)
              $('.goodsStorage i').text(goodsDetail.goodsStorage)
              $('.foot a').attr('href', 'tel:' + data.store.tel)
            }
          })
        }else {
          $('body').toast({
            content: data.msg
          })
        }
      },error: function () {
        $('body').toast({
          content: '系统错误'
        })
      }
    })
  }else {
    // 获取商品信息
    $.ajax({
      type: 'post',
      url: commonHost + '/index.php/mall/get_goods_detail',
      data: {goodsId: goodsId,tk: 0},
      success: function (data) {
        data = JSON.parse(data)
        if (data.ret == 1) {
          data = data.data
          var goodsDetail = data.goodsDetail
          // 截取图文详情中的html内容
          var start = goodsDetail.goodsDesc.indexOf('<body>') + 6
          var end = goodsDetail.goodsDesc.indexOf('</body>')
          // 获取轮播图片并追加到页面
          var banner = ''
          $(goodsDetail.goodsImg).each(function (i, v) {
            banner += "<div class='swiper-slide'>"
              + '<img src=' + v.img + '>'
              + '</div>'
            return banner
          })
          $('.swiper-wrapper').html(banner)
          var mySwiper = new Swiper('.swiper-container', {
            pagination: '.swiper-pagination',
            paginationType: 'fraction'
          })
          // 模板展示页面数据
          var html = template('goodsDetail', data)
          $('.main').html(html)
          // 图文详情数据展示
          var time = data.evaluate.gevalAddtime
          time = new Date(time).toLocaleString()
          $('.p-list i').text(time)
          var gevalScores = data.evaluate.gevalScores
          for (var i = 0;i < gevalScores;i++) {
            $('.mmm img').eq(i).attr('src', './images/star_red.png')
          }
          $('.text-image').append(goodsDetail.goodsDesc.substring(start, end))
          $('.smallImg img').attr('src', goodsDetail.goodsImg[0].img)
          $('iframe').html(goodsDetail.goodsDesc)
          $('.goodsPrice').text('￥' + goodsDetail.goodsPrice)
          $('.goodsStorage i').text(goodsDetail.goodsStorage)
        }else {
          $('body').toast({
            content: data.msg
          })
        }
      },error: function () {
        $('body').toast({
          content: '系统错误'
        })
      }
    })
  }

  // 点击分享及购买函数
  function dis (element) {
    var uid = $.cookie('uid')
    if (uid) {
      if (element == "$('.share')") {
        $('.shareLinks').show()
      }else if (element == "$('.purchase')") {
        $('.specificate').show()
      }
      $('.container').addClass('mask')
    }else {
      location.href = './certificate.html'
    }
  }

  // 详情页直接购买
  $('.purchase').click(function () {
    dis("$('.purchase')")
  })
  // 用户点击分享
  $('.share').click(function () {
    dis("$('.share')")
  })
  // 点击底部确认分享
  $('.shareBottom').click(function () {
    $('.shareLinks').hide()
    $('.container').removeClass('mask')
  })

  // 选择规格叉号点击
  $('.close').click(function () {
    $('.specificate').hide()
    $('.container').removeClass('mask')
  })

  // 选择规格
  $('.add').click(function () {
    var num = parseInt($('.num').text())
    num++
    $('.num').text(num)
  })

  $('.minus').click(function () {
    var num = parseInt($('.num').text())
    if (num == 1) {
      return
    }else {
      num--
      $('.num').text(num)
    }
  })

  $('.payment').click(function () {
    var num = parseInt($('.num').text())
    $.ajax({
      type: 'post',
      url: commonHost + '/index.php/cart/add_to_cart',
      data: {goodsId: $.cookie('goodsId'),num: num,tk: $.cookie('tk')},
      success: function (data) {
        data = JSON.parse(data)
        if (data.ret == 1) {
          location.href = './confirmOrder.html'
        }else {
          $('body').toast({
            content: data.msg
          })
        }
      },error: function () {
        $('body').toast({
          content: '系统错误'
        })
      }
    })
  })
})
